/*
 *  visrep.cpp
 *  BioBreak
 *
 *  Created by terry franguiadakis on 2/19/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#include "stdio.h" // $$$$ (TEF) TEMP for printf(<#const char * #>)
#include "core.h"
#include "visrep.h"
#include "render.h"

// -----------------------------------------------------------------------------

TestItemCircle::TestItemCircle(const Vector2f& i_pos, float i_radius)
:m_radius(i_radius) 
{
    SetPos(i_pos);
    m_transform.SetScale(i_radius);
}
                               

bool TestItemCircle::Hit(const Vector2f& i_pos)
{
    float dist = Dist(i_pos);
    
    if (dist <= m_radius)
    {
        m_isHit = true;
    }
    else
    {
        m_isHit = false;
    }
    
    return m_isHit;
}

void TestItemCircle::Render()
{
    Color3f color(1.0f, 0.0f, 0.0f);
    if (m_isHit)
    {
        color.r = 1.0f;
    }
    else 
    {
        color.r = 0.5f;
        color.g = 0.2f;
    }
        
    Vector2f unused;
    m_transform.PushMatrix();
    BasicDrawObjects::DrawSolidUnitCircle(color);
    m_transform.PopMatrix();
}



void TestItemCircle::RenderNoTransform()
{
    Color3f color(1.0f, 0.0f, 0.0f);
    if (m_isHit)
    {
        color.r = 1.0f;
    }
    else 
    {
        color.r = 0.5f;
        color.g = 0.2f;
    }
    
     BasicDrawObjects::DrawSolidUnitCircle(color);
   
}

// -----------------------------------------------------------------------------

void TestItemPoint::Render()
{
    Color3f color(0.0f, 1.0f, 0.0f);
    m_transform.PushMatrix();
    BasicDrawObjects::DrawPoint(Vector2f::Zero, m_size, color);
    m_transform.PopMatrix();
}

// -----------------------------------------------------------------------------
// QUAD

TestItemQuad::TestItemQuad(const Vector2f& i_pos, float i_size)
:m_size(i_size) 
{
    SetPos(i_pos); 
    m_transform.SetScale(m_size);
}

bool TestItemQuad::Hit(const Vector2f& i_pos)
{
    float dist = Dist(i_pos);
    
    if (dist <= (m_size * 0.5f))
    {
        m_isHit = true;
    }
    else
    {
        m_isHit = false;
    }
    
    return m_isHit;
}


void TestItemQuad::Render()
{
    Color3f color(0.0f, 0.0f, 1.0f);
    if (m_isHit)
    {
        color.r = 1.0f;
    }
    else 
    {
        color.r = 0.0f;
    }
    
    static Vector2f s_original[4];
    static bool isQuadSet = false;
    if (!isQuadSet)
    {
        s_original[0].Set(-0.5f, 0.5f);
        s_original[1].Set(0.5f, 0.5f);
        s_original[2].Set(0.5f, -0.5f);
        s_original[3].Set(-0.5f, -0.5f);
        isQuadSet = true;
    }
    
    m_transform.PushMatrix();
    BasicDrawObjects::DrawSolidPolygon(s_original, 4, color);
    m_transform.PopMatrix();
}

void TestItemQuad::RenderNoTransform()
{
    Color3f color(0.0f, 0.0f, 1.0f);
    if (m_isHit)
    {
        color.r = 1.0f;
    }
    else 
    {
        color.r = 0.0f;
    }
    
    static Vector2f s_original[4];
    static bool isQuadSet = false;
    if (!isQuadSet)
    {
        s_original[0].Set(-0.5f, 0.5f);
        s_original[1].Set(0.5f, 0.5f);
        s_original[2].Set(0.5f, -0.5f);
        s_original[3].Set(-0.5f, -0.5f);
        isQuadSet = true;
    }
    
    BasicDrawObjects::DrawSolidPolygon(s_original, 4, color);
}


// -----------------------------------------------------------------------------

void TestItemSegment::Render()
{
    Color3f color(1.0f, 1.0f, 0.0f);
    m_transform.PushMatrix();
    BasicDrawObjects::DrawSegment(Vector2f::Zero, m_toPos, color);
    m_transform.PopMatrix();
}

// -----------------------------------------------------------------------------

TestItemUIElement::TestItemUIElement(const Vector2f& i_pos, const float i_size, VisibleItem* i_shownItem)
:TestItemQuad(i_pos, i_size)
{
    m_visibleItemShown = i_shownItem;
 //  assert(i_shownItem != 0);
}

TestItemUIElement::~TestItemUIElement()
{
    delete m_visibleItemShown;
}

bool TestItemUIElement::Hit(const Vector2f& i_pos)
{
    bool isHit = TestItemQuad::Hit(i_pos);
    printf("test item hit %f %d\n", m_transform.GetPos().x, isHit);
    
    return isHit;
}

void TestItemUIElement::Render()
{
    m_transform.PushMatrix();
    TestItemQuad::RenderNoTransform();
    m_visibleItemShown->Render();
    m_transform.PopMatrix();
}

// -----------------------------------------------------------------------------

void TestTextureQuad::Render()
{
    Color4f color(1.0f, 1.0f, 1.0f, 1.0f);
       
    if (m_textureReference)
        m_textureReference->Apply();
    
    m_transform.PushMatrix();
    BasicDrawObjects::DrawTexturedRect(Vector2f(-0.5f, -0.5f), Vector2f(0.5f, 0.5f), color, Vector2f(0.0f, 0.0f), Vector2f(1.0f, 1.00f));
    m_transform.PopMatrix();
}

// -----------------------------------------------------------------------------
